home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Demo's / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Data Manipulation / MatrixToXYZ < prev    next >
Text File  |  1996-02-28  |  4KB  |  138 lines

  1. | MatrixToXYZ
  2. | This procedure file contains two macros to convert a 2-D matrix of Z values
  3. | into three separate X, Y, and Z waves
  4. | MatrixToXYZ, which converts the entire matrix into X, Y, and Z waves
  5. | and MatrixToXYZRange, which converts a given XY domain into X, Y, and Z waves.
  6.  
  7. #include <Multi-dimensional Utilities>
  8.  
  9. // Converts a 2-D matrix of Z values into three waves containing X, Y, and Z values
  10. // that spans the min and max X and Y.
  11. // The output waves are named by appending "X", "Y", and "Z" to the given basename.
  12. Macro MatrixToXYZ(mat,base,mktbl,mkgrf)
  13.     String mat,base="wave"
  14.     Variable mktbl=2,mkgrf=2
  15.     Prompt mat,"2D Matrix Wave",popup,WaveList("*",";","")
  16.     Prompt base,"Output wave basename (outputs have X, Y, and Z suffixes)"
  17.     Prompt mktbl,"Put waves in new table?",popup,"Yes;No"
  18.     Prompt mkgrf,"Display waves in new graph?",popup,"Yes;No"
  19.  
  20.     Silent 1;PauseUpdate
  21.     if( WaveDims($mat) != 2)
  22.         Abort mat+" is not a two-dimensional wave!"
  23.     endif
  24.     
  25.     // Determine full X and Y Ranges
  26.     Variable rows=DimSize($mat,0)
  27.     Variable cols=DimSize($mat,1)
  28.     Variable xmin,ymin,dx,dy
  29.     xmin=DimOffset($mat,0)
  30.     dx=DimDelta($mat,0)
  31.     ymin=DimOffset($mat,1)
  32.     dy=DimDelta($mat,1)
  33.     
  34.     // Make X, Y, and Z waves
  35.     String wx=base+"X"
  36.     String wy=base+"Y"
  37.     String wz=base+"Z"
  38.     Make/O/N=(rows*cols) $wx,$wy,$wz
  39.     $wx= xmin + dx * mod(p,rows)        // X varies quickly
  40.     $wy= ymin + dy * floor(p/rows)    // Y varies slowly
  41.     $wz= $mat($wx[p])($wy[p])
  42.     Preferences 1
  43.     if( mktbl == 1)
  44.         Edit $wx,$wy,$wz
  45.     endif
  46.     if( mkgrf == 1)
  47.         Display $wx,$wy,$wz
  48.     endif
  49. End
  50.  
  51. // Converts 2-D matrix of Z values into three waves into
  52. // containing X, Y, and Z values that span the given (or auto) X and Y range
  53. Macro MatrixToXYZRange(mat,base,minx,maxx,miny,maxy,mktbl,mkgrf)
  54.     String mat,base="wave"
  55.     Variable minx=NaN,maxx=NaN,miny=NaN,maxy=NaN
  56.     Variable mktbl=2,mkgrf=2
  57.     Prompt mat,"2D Matrix Wave",popup,WaveList("*",";","")
  58.     Prompt base,"basename for X, Y, Z output waves"
  59.     Prompt minx,"matrix min X, or NaN for auto min X"
  60.     Prompt maxx,"matrix max X, or NaN for auto max X"
  61.     Prompt miny,"matrix min Y, or NaN for auto min Y"
  62.     Prompt maxy,"matrix max  Y, or NaN for auto max Y"
  63.     Prompt mktbl,"Put waves in new table?",popup,"Yes;No"
  64.     Prompt mkgrf,"Display waves in new graph?",popup,"Yes;No"
  65.  
  66.     Silent 1;PauseUpdate
  67.     if( WaveDims($mat) != 2)
  68.         Abort mat+" is not a two-dimensional wave!"
  69.     endif
  70.     
  71.     // Determine full X and Y Ranges
  72.     Variable rows=DimSize($mat,0)    // rows in entire matrix
  73.     Variable cols=DimSize($mat,1)        // columns in entire matrix
  74.     Variable xmin,xmax,ymin,ymax,dx,dy
  75.     xmin=DimOffset($mat,0)
  76.     dx=DimDelta($mat,0)
  77.     xmax=xmin+dx*(rows-1)
  78.     ymin=DimOffset($mat,1)
  79.     dy=DimDelta($mat,1)
  80.     ymax=ymin+dy*(cols-1)
  81.     
  82.     // Substitute auto values where requested
  83.     if( numtype(minx)!=0 )
  84.         minx= xmin    // use auto min
  85.     endif
  86.     if( numtype(maxx)!=0 )
  87.         maxx= xmax
  88.     endif
  89.     if( numtype(miny)!=0 )
  90.         miny= ymin
  91.     endif
  92.     if( numtype(maxy)!=0 )
  93.         maxy= ymax
  94.     endif
  95.     string range
  96.     // check user's Xs
  97.     sprintf range,"x min=%g, x max=%g",xmin,xmax
  98.     if( limit(minx,xmin,xmax)!=minx)
  99.         Abort "min X isn't between "+range
  100.     endif
  101.     if( limit(maxx,xmin,xmax)!=maxx)
  102.         Abort "max X isn't between "+range
  103.     endif
  104.     // Check Ys
  105.     sprintf range,"y min=%g, y max=%g",ymin,ymax
  106.     if( limit(miny,ymin,ymax)!=miny)
  107.         Abort "min Y isn't between "+range
  108.     endif
  109.     if( limit(maxy,ymin,ymax)!=maxy)
  110.         Abort "max Y isn't between "+range
  111.     endif
  112.     // Determine the equivalent row and column ranges
  113.     Variable firstRow= x2pntMD($mat,0,minx)
  114.     Variable lastRow= x2pntMD($mat,0,maxx)
  115.     Variable firstCol= x2pntMD($mat,1,miny)
  116.     Variable lastCol= x2pntMD($mat,1,maxy)
  117.     Variable xyzrows=lastRow-firstRow+1
  118.     Variable xyzcols=lastCol-firstCol+1
  119.     minx= pnt2xMD($mat,0,firstRow)        // make sure minx corresponds exactly to a matrix row
  120.     miny= pnt2xMD($mat,1,firstCol)        // make sure minyx corresponds exactly to a matrix column
  121.     
  122.     // Make X, Y, and Z waves
  123.     String wx=base+"X"
  124.     String wy=base+"Y"
  125.     String wz=base+"Z"
  126.     Make/O/N=(xyzrows*xyzcols) $wx,$wy,$wz
  127.     $wx= minx + dx * mod(p,xyzrows)        // X varies quickly
  128.     $wy= miny + dy * floor(p/xyzrows)    // Y varies slowly
  129.     $wz= $mat($wx[p])($wy[p])
  130.     Preferences 1
  131.     if( mktbl == 1)
  132.         Edit $wx,$wy,$wz
  133.     endif
  134.     if( mkgrf == 1)
  135.         Display $wx,$wy,$wz
  136.     endif
  137. End
  138.